﻿/* 
 *  <copyright file="Camera3DCmp.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SobrietyEngine.Components.Camera
{
    public class Camera3DCmp: BaseCamera, IComponent, ICamera
    {
        GraphicsDevice graphicsDevice;
        float aspectRatio;

        public Camera3DCmp(GraphicsDevice gDevice)
            : base()
        {
            graphicsDevice = gDevice;
            aspectRatio = gDevice.Viewport.AspectRatio; ;
        }

        //for code base creation
        public Camera3DCmp(GraphicsDevice gDevice, Viewport _viewport, Matrix _viewScale)
            : base()
        {
            graphicsDevice = gDevice;
            aspectRatio = _viewport.AspectRatio;

            Viewport = _viewport;
            ViewportScale = _viewScale;
        }

        public override Boolean InitComponent()
        {
            base.InitComponent();
            ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.PiOver4, aspectRatio, 0.1f, 10000.0f);//0.2f, 500.0f);

            BoundingFrustum = new BoundingFrustum(Matrix.Identity);

            return true;
        }

        override public Type[] SupportedInterfaces()
        {
            return new Type[] { typeof(ICamera) };
        }

        Vector3 _camPos = new Vector3();
        Vector3 _lookAt = new Vector3();
        public override Matrix GetViewMatrix()
        {
            //gets world to screen transformation
            //return Matrix.CreateTranslation(new Vector3(-1 * Lens.X, -1 * Lens.Y, 0)) * ViewportScale;
            _camPos.X = Position.X;
            _camPos.Y = Position.Y;
            _camPos.Z = Position.Z;

            //_lookAt = _camPos;
           // _lookAt.Z = -2;
            _lookAt = _camPos + orientationCmp.Forward;
            return Matrix.CreateLookAt(_camPos, _lookAt, orientationCmp.Up);
        }

        public override BoundingFrustum GetBoundingFrustum()
        {
            return new BoundingFrustum(Matrix.Multiply(GetViewMatrix(), ProjectionMatrix));
        }
    }
}